<template>
  <el-dialog title="编辑" class="gl_dialog_TitleLine gl_dl_footer" :visible.sync="dialogVisible" :before-close="close" width="660px">
    <div class="gl_my_30">
      <el-form ref="form" label-position="right" :rules="rules" style="width:60%" label-width="150px" :model="formData">
        <el-form-item prop="newPassword" label="设定新密码">
          <el-input v-model="formData.newPassword" type="password" placeholder="请输入新密码" size="mini" />
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="close()">取 消</el-button>
      <el-button :loading="loading" size="mini" type="primary" @click="Edit()">保 存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import validator from '@/lib/validate/index.js';
import { translateByCode } from '@/lib/getTransData';
import {
  api_PUT_UsersIdResetPassword // 重置用户密码
} from '@/extend/api/swaggerApi/identity.js';
export default {
  props: {
    currentRegister: {
      type: String,
      required: false,
      default: ''
    }
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      rules: {
        newPassword: [
          validator.required(translateByCode('rules.requiredPassword')),
          {
            validator(rule, value, callback) {
              callback(validator.checkPwd(value, translateByCode('rules.validPassword')));
            },
            trigger: 'blur'
          }
        ]
      },
      formData: { newPassword: '' }
    };
  },
  methods: {
    // 打开弹窗
    open() {
      this.dialogVisible = true;
    },
    // 关闭弹窗
    close() {
      this.$refs['form'].resetFields();
      this.dialogVisible = false;
    },
    // 新增 or 编辑
    Edit() {
      var that = this;
      that.$refs.form.validate((valid) => {
        if (valid) {
          that.loading = true;
          api_PUT_UsersIdResetPassword(that.currentRegister, that.formData).then(res => {
            that.loading = false;
            that.dialogVisible = false;
            that.$message({ type: 'success', message: '设定密码成功!' });
          }).catch(() => {
            that.loading = false;
            that.$message({ type: 'error', message: '设定密码失败' });
          });
          that.$refs['form'].resetFields();
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>

</style>
